package com.cctc.juc.demo.Bitc.Tmp.ADemo;

import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;

public class FSemaphoreRunDemo {

    // 效果 5： A~Z 每一时刻最多有两个任务并发执行。

    private static Semaphore semaphore = new Semaphore(2);

    public static void main(String[] args) {
        for (char ch = 'A'; ch <= 'Z'; ch++) {
            final String taskName = String.valueOf(ch);
            new Thread(() -> {
                try {
                    semaphore.acquire();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.print(taskName + " ");
                semaphore.release();
            }).start();
        }
    }

}
